$(function(){
    var index = new Index();
    index.init().init();
    
    showBoxH(500);
});


function Index(){
    this.init = function(){
        return INDEX;
    };
    
    var INDEX = {
        init : function(){
            $.util.initAppTagLink($('span.s_app_list_tag'), SYSTEM_DATA.contextPath);
            INDEX.listeners.bindEvents();
            
        },
        listeners : {
            bindEvents : function(){
                $.util.foreachFunction(this, [ "bindEvents" ]);
            },
            bindRandScrollData : function(){
                var $scroller = $('#div_scroller');
                $scroller.everyTime('10s', function(){
                    $.post(SYSTEM_DATA.contextPath + '/Index/listWeDoing.shtml', {
                                            
                        }, function(jsonResult){
                            var data = jsonResult.data;
                                                
                            var $li = $scroller.find('li').last();
                                                
                            $li.children('img').attr('src', data.app.app_icon);
                            var name = data.app[0].name;
                            if(20 < name.length){
                                name = name.substring(0, 20) + '...';
                            }
                            $li.find('div>p>a').attr('href', SYSTEM_DATA.contextPath + '/Index/appInfo/id/'+ data.app[0].id +'.shtml');
                            $li.find('div>p>a').text(name);
                            $li.find('div>span').text(data.datetime);
                        }, 'json');
                        
                });  
            },
            bindScroll : function(){
                var _wrap=$('#div_scroller ul');//定义滚动区域
                var _interval=5000;//定义滚动间隙时间
                var _moving;//需要清除的动画
                _wrap.hover(function(){
                    clearInterval(_moving);//当鼠标在滚动区域中时,停止滚动
                },function(){
                    _moving=setInterval(function(){
                        var _field=_wrap.find('li:first');//此变量不可放置于函数起始处,li:first取值是变化的
                        var _h=_field.height();//取得每次滚动高度(多行滚动情况下,此变量不可置于开始处,否则会有间隔时长延时)
                        _field.animate({
                            marginTop:-_h+'px'
                        },600,function(){//通过取负margin值,隐藏第一行
                            _field.css('marginTop',0).appendTo(_wrap);//隐藏后,将该行的margin值置零,并插入到最后,实现无缝滚动
                        })
                    },_interval)//滚动间隔时间取决于_interval
                }).trigger('mouseleave');//函数载入时,模拟执行mouseleave,即自动滚动
                
            },
            bindAppInfo : function(){
                $('a.a_index_app_info').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_index'),SYSTEM_DATA.contextPath + '/Index/appInfo.shtml',  [{
                        'name' : 'id', 
                        'value' : $(this).attr('a_id')
                    }]);
                });
            },
            //最新游戏，最新应用
            bindIndexNewTable : function(){
                $('#newGameGuideOver a').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_index'),SYSTEM_DATA.contextPath + '/Index/index.shtml',  [{
                        'name' : 'type', 
                        'value' : $(this).attr('type')
                    }]);
                });
            },
            bindAppTag : function(){
                $('a.a_app_list_tag').click(function(){
                    $.util.initHiddenFormSubmit($('#frm_hidden_app_index'), SYSTEM_DATA.contextPath + '/Index/appTypeList.shtml', [{
                        'name' : 'type', 
                        'value' : $(this).attr('tag_text')
                    }]);
                });
            },
            bindAdClick : function(){
                $("a.a_index_ad").click(function(){
                    var $this = $(this);
                    $.post(SYSTEM_DATA.contextPath + '/Ad/countAdClickRate', {
                        'id' : $this.attr('ad_id')
                    }, function(jsonResult){}, 'json');
                });
            },
            bindDownLoadMobile : function(){
                $("a.app-send").click(function(){
                    $("#yui_3_3_0_8_132064797788612").dialog({
                        'title' : '发送到手机'
                    }
                    );
                });
            },
            bindShowDetail : function(){
                //                console.debug($("div.newsDiv a"));
                $("#scroll-apps div.app-logo-big").poshytip({
                    content: function(updateCallback) {
                        var $this = $(this);
                        window.setTimeout(function() {
                            var $detail = $this.next().next();
                            //                            console.debug($detail);
                            $detail.removeClass('hidden');
                            updateCallback($detail);
                        }, 1000);
                        return 'Loading...';
                    },
                    className: 'tip-yellowsimple',
                    alignX: 'center',
                    alignY: 'top',
                    bgImageFrameSize: 9,
                    offsetX: 0,
                    offsetY: 20
                });
            },
            bindLinkTitle : function(){
            //                $('div.newsDiv a').poshytip({
            //                    className: 'tip-skyblue',
            //                    bgImageFrameSize: 9,
            //                    offsetX: 0,
            //                    offsetY: 20
            //                });
            }
        },
        funs : {}
    };
}


function showBoxH(speed){
    var $cur = 1;
    var $i = 1;
    var $len = $('.animation-h-box .showbox-h ul li.scroll_ad').length;
    //alert('len = ' + $len);
    var $pages = Math.ceil($len / $i);
    var $w = $('.animation-h-box').width();
    var $showbox = $('.showbox-h');
    var $num = $('.scroll-paging .num li')
    var $pre = $('.showbox-h-left')
    var $next = $('.showbox-h-right');
    var $speed = speed;
    //向前滚动
    $pre.click(function(){
        if (!$showbox.is(':animated')) { 
            if ($cur == 1) { 
                $showbox.animate({
                    left: '-=' + $w * ($pages - 1)
                }, $speed); 
                $cur = $pages; 
            }
            else {
                $showbox.animate({
                    left: '+=' + $w
                }, $speed); 
                $cur--; 
            }
            $num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); 
        }
    });
    //向后滚动
    $next.click(function(){
        if (!$showbox.is(':animated')) { 
            // alert($pages);
            if ($cur == $pages) { 
                // alert(10);
                $showbox.animate({
                    left: 0
                }, $speed); 
                $cur = 1; 
            }
            else {
                $showbox.animate({
                    left: '-=' + $w
                }, $speed);
                $cur++; 
            }
            
            bindAnimationImgs($cur);
            $num.eq($cur - 1).addClass('numcur').siblings().removeClass('numcur'); 
        }
    });
    //数字点击事件
    $num.click(function(){
        if (!$showbox.is(':animated')) { 
            var $index = $num.index(this); 
            $showbox.animate({
                left: '-' + ($w * $index)
            }, $speed);
            $cur = $index + 1; 
            bindAnimationImgs($cur);
            $(this).addClass('numcur').siblings().removeClass('numcur'); 
        }
    });
    var _moving;
    var $boxes = $showbox.add($num).add($pre).add($next);
    $boxes.mouseover(function(){
        clearInterval(_moving);
    }).mouseout(function(){
        _moving=setInterval(function(){
            $next.click();
        },6000);
    });
    $showbox.trigger('mouseout');
}

function bindAnimationImgs(pageNum){
    var page = $(".animation-h-box .showbox-h li")[pageNum - 1];
    var logos = $(page).find(".app-logo-big");
    if("none" != logos.css("background-image")){
        return;
    }
    $.each(logos,function(i){
        $(this).attr("style",$(this).attr("original"));
    });
}